package sparrowGui.item
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import sparrowGui.Component;
	
	/**
	 * 将mc帧转成鼠标事件的item按钮
	 * @author Pelephone
	 */
	public class McBtn extends Item
	{
		public static const UP_STATE:int = 1;
		public static const SELECT_STATE:int = 2;
		public static const OVER_STATE:int = 3;
		public static const DOWN_STATE:int = 4;
		public static const DISABLE_STATE:int = 5;
		
		private var _disable:Boolean = false;
		
		public function McBtn(data:Object, argSkin:MovieClip,vars:Object=null)
		{
			if(argSkin.totalFrames<2) new Error("组件皮肤帧数错误!");
			super(data, argSkin,vars);
			argSkin.gotoAndStop(UP_STATE);
		}

		override protected function draw():void
		{
			if(style.isBgFill && skin){
				skin.width = style.width;
				skin.height = style.height;
			}
			var mc:MovieClip = skin as MovieClip;
			if(!selected) mc.gotoAndStop(UP_STATE);
			else mc.gotoAndStop(selectState);
			dispatchEvent(new Event(Component.COMPONENT_DRAW));
		}
	
		override public function onMouse(e:Event):void
		{
			var mc:MovieClip = skin as MovieClip;
			switch(e.type){
				case MouseEvent.ROLL_OVER:
					if(!selected && mc.currentFrame!=DOWN_STATE)
						mc.gotoAndStop(OVER_STATE);
					break;
				case MouseEvent.MOUSE_DOWN:
					if(selected) return;
					mc.gotoAndStop(DOWN_STATE);
					if(!stage) return;
					stage.addEventListener(MouseEvent.MOUSE_UP, onMouse);
					stage.addEventListener(Event.MOUSE_LEAVE,onMouse);
					break;
				case MouseEvent.MOUSE_UP:
				case Event.MOUSE_LEAVE:
					if(selected) return;
					mc.gotoAndStop(UP_STATE);
					if(!stage) return;
					stage.removeEventListener(MouseEvent.MOUSE_UP, onMouse);
					stage.removeEventListener(Event.MOUSE_LEAVE, onMouse);
					break;
				case MouseEvent.MOUSE_OUT:
					if(!selected && mc.currentFrame!=DOWN_STATE)
						mc.gotoAndStop(UP_STATE);
					break;
			}
		}
		
		override public function set selected(value:Boolean):void
		{
			_selected = value;
			var mc:MovieClip = skin as MovieClip;
			removeEvt();
			if(_selected){
				mc.gotoAndStop(SELECT_STATE);
			}else{
				addEvt();
				mc.gotoAndStop(UP_STATE);
			}
		}
		
		override public function get selected():Boolean
		{
			return _selected;
		}
		
		
		public function get disable():Boolean
		{
			return _disable;
		}
		
		public function set disable(value:Boolean):void
		{
			_disable = value;
			removeEvt();
			var mc:MovieClip = skin as MovieClip;
			if(_disable){
				mc.gotoAndStop(DISABLE_STATE);
			}else{
				addEvt();
				mc.gotoAndStop(UP_STATE);
			}
		}
	}
}